﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/VipLevel")]
    public class VipLevelController : ApiController
    {
        B_VipLevel b;
        public VipLevelController()
        {
            b = new B_VipLevel();
        }
        /// <summary>
        /// 获取所有超级会员
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData()
        {
            var data = from v in b.GetAll()
                       select new VipLevel
                       {
                           LevelID = v.LevelID,
                           VipName = v.VipName,
                           VipPrice=v.VipPrice,
                           VipDay = v.VipDay,
                           VStorageNum = v.VStorageNum
                       };
            return Ok(new Result<List<VipLevel>> { Data = data.ToList() });
        }

        /// <summary>
        /// 获取所有超级会员(分页)
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData(int page, int size)
        {
            var data = from v in b.GetAll()
                       select new VipLevel
                       {
                           LevelID = v.LevelID,
                           VipName = v.VipName,
                           VipPrice = v.VipPrice,
                           VipDay = v.VipDay,
                           VStorageNum = v.VStorageNum
                       };
            return Ok(new Result<List<VipLevel>>
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }

        /// <summary>
        /// 添加超级会员
        /// </summary>
        /// <param name="VipLevel"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(VipLevel level)
        {
            if (b.GetAll().Count(x=>x.VipName==level.VipName)==0)
            {
                b.Add(new VipLevel
                {
                    VipName = level.VipName,
                    VipPrice = level.VipPrice,
                    VipDay = level.VipDay,
                    VStorageNum = level.VStorageNum
                });
                return Ok(new Result<VipLevel> { });
            }
            else
            {
                return Ok(new Result<object>
                {
                    Code = 403,
                    Msg = "出错，请联系管理员"
                });
            }
        }

        /// <summary>
        /// 删除超级会员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                b.Delete(id);
                return Ok(new Result<object>
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "该用户已删除"
                });
            }
        }


        /// <summary>
        /// 查找所有超级会员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Find/{id}")]
        [HttpGet]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = b.FindById(id);
                if (b.FindById(id)==null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据走丢了"
                    });
                }
                return Ok(new Result<VipLevel>
                {
                    Data = new VipLevel
                    {
                        LevelID=data.LevelID,
                        VipName = data.VipName,
                        VipPrice = data.VipPrice,
                        VipDay = data.VipDay,
                        VStorageNum = data.VStorageNum
                    }
                });
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }

        /// <summary>
        /// 更新超级会员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id,VipLevel level)
        {
            VipLevel v = new VipLevel
            {
                VipName = level.VipName,
                VipPrice = level.VipPrice,
                VipDay = level.VipDay,
                VStorageNum = level.VStorageNum
            };
            try
            {
                b.Update(id,level);
                return Ok(new Result<object>());
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Data = 500,
                    Msg = "更新失败"
                });
            }
        }
    }
}
